import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/components/LoginWindow.vue'
import ChatWindow from '@/components/ChatWindow.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginPage
  },
  {
    path: '/chat',
    name: 'chat',
    component: ChatWindow,
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !localStorage.getItem('isAuthenticated')) {
    next('/')
  } else {
    next()
  }
})

export default router